﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000298 RID: 664
	internal class DoOnCompletedObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000EAF RID: 3759 RVA: 0x000450B4 File Offset: 0x000434B4
		public DoOnCompletedObservable(IObservable<T> source, Action onCompleted) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.onCompleted = onCompleted;
		}

		// Token: 0x06000EB0 RID: 3760 RVA: 0x000450D0 File Offset: 0x000434D0
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new DoOnCompletedObservable<T>.DoOnCompleted(this, observer, cancel).Run();
		}

		// Token: 0x04000886 RID: 2182
		private readonly IObservable<T> source;

		// Token: 0x04000887 RID: 2183
		private readonly Action onCompleted;

		// Token: 0x02000299 RID: 665
		private class DoOnCompleted : OperatorObserverBase<T, T>
		{
			// Token: 0x06000EB1 RID: 3761 RVA: 0x000450DF File Offset: 0x000434DF
			public DoOnCompleted(DoOnCompletedObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000EB2 RID: 3762 RVA: 0x000450F0 File Offset: 0x000434F0
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06000EB3 RID: 3763 RVA: 0x00045103 File Offset: 0x00043503
			public override void OnNext(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000EB4 RID: 3764 RVA: 0x00045114 File Offset: 0x00043514
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000EB5 RID: 3765 RVA: 0x0004514C File Offset: 0x0004354C
			public override void OnCompleted()
			{
				try
				{
					this.parent.onCompleted();
				}
				catch (Exception error)
				{
					this.observer.OnError(error);
					base.Dispose();
					return;
				}
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000888 RID: 2184
			private readonly DoOnCompletedObservable<T> parent;
		}
	}
}
